//
//  CarPhotoViewController.m
//  LocomotiveGuards
//
//  Created by zwj on 15-11-23.
//  Copyright (c) 2015年 REDOOR. All rights reserved.
//

#import "CarPhotoViewController.h"
#import "BuyDateViewController.h"
@interface CarPhotoViewController () <UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    UIButton *_btnLast;
    UIButton *_btnNext;
    UIButton *_btnSkip;
    UIButton *_btnUploadImage;
    UILabel *_labelPhoto;
    UIImageView *_imgViewPhoto;
    UIButton *_btnChangePhoto;
}
@end

@implementation CarPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTitle:@"注册-车辆验证"];
    [self configView];
}


- (void)configView
{
    //电动车图片label
    UILabel *nameLabel = [self createLabelFrame:CGRectMake(30*W, 110*H, SCREEN_WIDTH-60*W, 30*H) title:@"电动车图片" font:[UIFont systemFontOfSize:26]];
    [self.view addSubview:nameLabel];
    
    //拍照按钮
    _btnUploadImage = [self createBtnFrame:CGRectMake((SCREEN_WIDTH-80*W)/2, SCREEN_HEIGHT/2-60*H, 80*W, 60*H) title:nil bgImageName:@"11pic_upload01" selectedImageName:@"11pic_upload01" target:self action:@selector(photograph)];
    [self.view addSubview:_btnUploadImage];
    
    //拍照上传label
    _labelPhoto = [self createLabelFrame:CGRectMake((SCREEN_WIDTH-60*W)/2, SCREEN_HEIGHT/2+10*H, 60*W, 20*H) title:@"拍照上传" font:[UIFont systemFontOfSize:12]];
    [self.view addSubview:_labelPhoto];
    
    //照片显示View
    _imgViewPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(30*W,  SCREEN_HEIGHT/2-85*H, SCREEN_WIDTH-60*W, 170*H)];
    CALayer *viewLayer = _imgViewPhoto.layer;
    [viewLayer setMasksToBounds:YES];
    viewLayer.cornerRadius = 15;
    [self.view addSubview:_imgViewPhoto];
    _imgViewPhoto.hidden = YES;
    
    //更改照片按钮
    _btnChangePhoto = [self createBtnFrame:CGRectMake((SCREEN_WIDTH-80*W)/2, CGRectGetMaxY(_imgViewPhoto.frame)+15*H, 80*W, 28*H) title:nil bgImageName:@"12pic_btn01" selectedImageName:@"12pic_btn02" target:self action:@selector(changePhoto)];
    [self.view addSubview:_btnChangePhoto];
    _btnChangePhoto.hidden = YES;
    
    //上一步按钮
    _btnLast = [self addLastBtnTarget:self action:@selector(btnLastClicked:)];
    [self enableButton:_btnLast];
    
    //跳过按钮
    _btnSkip = [self addNextBtnTarget:self action:@selector(skipClicked:)];
    [_btnSkip setTitle:@"跳过" forState:UIControlStateNormal];
}

#pragma mark - 点击事件
//上一步，返回上一个界面
- (void)btnLastClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//跳过上传电动车照片界面
- (void)skipClicked:(id)sender
{
    BuyDateViewController *bdvc = [BuyDateViewController new];
    [self.navigationController pushViewController:bdvc animated:YES];
}

//下一步，进入新页面
- (void)btnNextClicked:(id)sender
{
    BuyDateViewController *bdvc = [BuyDateViewController new];
    [self.navigationController pushViewController:bdvc animated:YES];
}

//拍照响应事件
- (void)photograph
{
    UIActionSheet *acSheet=[[UIActionSheet alloc]initWithTitle:@"选择图片来源" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从手机相册选择", nil];
    [acSheet showInView:self.view];
}

//更改照片点击事件
- (void)changePhoto
{
    UIActionSheet *acSheet=[[UIActionSheet alloc]initWithTitle:@"选择图片来源" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从手机相册选择", nil];
    [acSheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //调用相机
    if (buttonIndex==0) {
        NSLog(@"调用相机拍照");
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [self transferWithType:UIImagePickerControllerSourceTypeCamera];
        }else{
            NSLog(@"调用相机失败");
        }
    }
    //调用相册
    else if (buttonIndex==1){
        NSLog(@"调用相册");
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [self transferWithType:UIImagePickerControllerSourceTypePhotoLibrary];
        }else{
            NSLog(@"调用相册失败");
        }
    }
}

//跳转方式(相机 or 相册？)
- (void)transferWithType:(UIImagePickerControllerSourceType)type{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = type;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:^{
    }];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"cancel");
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self saveImage:image withName:@"ebicyclePic.png"];
        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"ebicyclePic.png"];
        UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
        [_imgViewPhoto setImage:savedImage];
    }
    [picker dismissViewControllerAnimated:YES completion:^{
        [_btnSkip removeFromSuperview];
        _btnUploadImage.hidden = YES;
        _labelPhoto.hidden = YES;
        _imgViewPhoto.hidden = NO;
        _btnChangePhoto.hidden = NO;
        [self addNextBtnTarget:self action:@selector(btnNextClicked:)];
    }];
}

#pragma mark - 保存图片至沙盒
- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    NSLog(@"%@", fullPath);
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
